package chat.activities;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import chat.fragments.ControlFragment;
import chat.fragments.RoomFragment;
import chat.fragments.NotifyFragment;
import chat.fragments.UserFragment;

/**
 * 聊天主界面
 * Created by 凯 on 2015/10/24.
 */
public class IMActivity extends UIHandler.BaseActivity implements
        ControlFragment.Delegate {

    public static final String TAG_FRIEND = "friend";
    public static final String TAG_GROUP = "group";
    public static final String TAG_CONTROL = "control";
    public static final String TAG_NOTIFY = "notify";


    // 可以切换界面
    Fragment fragment_friend;
    Fragment fragment_group;
    Fragment fragment_contorl;
    Fragment fragment_notify;

    // 切换界面
    View view_bottom_friend;
    View view_bottom_group;
    View view_bottom_contorl;
    View view_bottom_notify;

    // 未读消息数目
    TextView tv_unread;

    // 底部图片
    ImageView img_msg;
    ImageView img_friend;
    ImageView img_group;
    ImageView img_setting;

    // 返回
    Button btn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im);

        initViews();

        if (savedInstanceState == null) {
            initFragement();
        }

        // 初始化获取用户信息


    }

    // 底部点击事件
    View.OnClickListener bottomClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == view_bottom_friend) {
                show(fragment_friend);
                setallgrey();
                img_friend.setImageResource(R.drawable.friend_blue);

            } else if (v == view_bottom_group) {
                show(fragment_group);
                setallgrey();
                img_group.setImageResource(R.drawable.group_icon_blue);

            } else if (v == view_bottom_contorl) {
                show(fragment_contorl);
                setallgrey();
                img_setting.setImageResource(R.drawable.setting_blue);
            } else if (v == view_bottom_notify) {
                show(fragment_notify);
                setallgrey();
                img_msg.setImageResource(R.drawable.msg_blue);
            } else if (v == btn_back) {
                finish();
            }
        }
    };

    void setallgrey() {
        img_msg.setImageResource(R.drawable.msg_grey);
        img_friend.setImageResource(R.drawable.friend_grey);
        img_group.setImageResource(R.drawable.group_icon_grey);
        img_setting.setImageResource(R.drawable.setting_grey);
    }


    // 初始化界面
    private void initViews() {
        // 好友
        view_bottom_friend = findViewById(R.id.id_bottom_friend);
        view_bottom_friend.setOnClickListener(bottomClick);

        // 群组
        view_bottom_group = findViewById(R.id.id_bottom_group);
        view_bottom_group.setOnClickListener(bottomClick);

        // 控制
        view_bottom_contorl = findViewById(R.id.id_bottom_control);
        view_bottom_contorl.setOnClickListener(bottomClick);

        // 通知消息
        view_bottom_notify = findViewById(R.id.id_bottom_notify);
        view_bottom_notify.setOnClickListener(bottomClick);

        // 未读消息组件
        tv_unread = (TextView) findViewById(R.id.id_tv_unread);

        // 底部图片
        img_msg = (ImageView) findViewById(R.id.id_img_msg);
        img_friend = (ImageView) findViewById(R.id.id_img_friend);
        img_group = (ImageView) findViewById(R.id.id_img_group);
        img_setting = (ImageView) findViewById(R.id.id_img_setting);

        // 返回图标
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(bottomClick);
    }


    // 初始化fragment
    private void initFragement() {

        img_msg.setImageResource(R.drawable.msg_blue);

        fragment_friend = new UserFragment();
        fragment_group = new RoomFragment();
        fragment_contorl = new ControlFragment();
        fragment_notify = new NotifyFragment();


        FragmentManager fm = getFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
//        tx.add(R.id.id_content, fragment_friend, TAG_FRIEND);
//        tx.add(R.id.id_content, fragment_group, TAG_GROUP);
//        tx.add(R.id.id_content, fragment_contorl, TAG_GROUP);
//        tx.add(R.id.id_content, fragment_notify, TAG_NOTIFY);
//        tx.hide(fragment_friend);
//        tx.hide(fragment_group);
//        tx.hide(fragment_contorl);
        tx.replace(R.id.id_content,fragment_notify,TAG_NOTIFY);
        tx.commit();
    }


    // 显示某块
    public void show(Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
//        tx.hide(fragment_friend);
//        tx.hide(fragment_group);
//        tx.hide(fragment_contorl);
//        tx.hide(fragment_notify);
//        tx.show(fragment);
        tx.replace(R.id.id_content,fragment);
        tx.commit();
    }


    // 上传图片的任务
    public class UploadPhotoTask extends AsyncTask<Void,Integer,Integer>
    {

        @Override
        protected Integer doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);

        }
    }

}
